//
//  SurveyViewController.m
//  Surveyer
//
//  Created by Qikun Guo on 13-7-25.
//  Copyright (c) 2013年 Tim. All rights reserved.
//

#import "SurveyViewController.h"
#import "DetailViewControllers.h"
#import "TypeRedirector.h"

@interface SurveyViewController ()

@end

@implementation SurveyViewController
@synthesize surveyView, progressView, undoManager, currentSurvey, pageController;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    }
    return self;
}

- (BOOL)shouldAutorotate
{
    return NO;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    viewControllers = [[NSMutableOrderedSet alloc] init];
    
    count = currentSurvey.items.count + 2;
    progressView.progress = 1.0/count;
    
    pageController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
    pageController.delegate = self;
    pageController.dataSource = self;
    pageController.view.frame = surveyView.frame;
    
    beginVC = [[dBeginViewController alloc] init];
    ((dBeginViewController *)beginVC).currentSurvey = currentSurvey;
    [viewControllers addObject:beginVC];
    
    
    NSEnumerator *enumerator = currentSurvey.items.objectEnumerator;
    SurveyItem *temp;
    dViewController *dvc;
    while ((temp = enumerator.nextObject)) {
        dvc = [TypeRedirector viewControllerOfItem:temp];
        dvc.delegate = self;
        [viewControllers addObject:dvc];
    }
    
    endVC = [[dEndViewController alloc] init];
    ((dBeginViewController *)endVC).currentSurvey = currentSurvey;
    endVC.delegate = self;
    [viewControllers addObject:endVC];
    [pageController setViewControllers:@[beginVC] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
    [self addChildViewController:pageController];
    [surveyView addSubview:pageController.view];
    [pageController didMoveToParentViewController:self];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Page view
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
    NSUInteger index = [viewControllers indexOfObject:viewController];
    if (index == count-1)
        return nil;
    return [viewControllers objectAtIndex:index+1];
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
    NSUInteger index = [viewControllers indexOfObject:viewController];
    if (index == 0)
        return nil;
    return [viewControllers objectAtIndex:index-1];
}

- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController
{
    return count;
}

- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController
{
    return 0;
    //return [(dViewController *)([pageViewController.viewControllers objectAtIndex:0]) index];
}

- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed
{
    //NSLog(@"%d", [(dViewController *)([pageViewController.viewControllers objectAtIndex:0]) index] / count);
    progressView.progress = ([viewControllers indexOfObject:[pageViewController.viewControllers objectAtIndex:0]] +1) / (float)count;
}

- (void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray *)pendingViewControllers
{
    
}

#pragma mark - IBAction & @Selector
- (IBAction)undoAction:(id)sender
{
    [[[SurveyerStore defaultStore] undoManager] undo];
    [pageController.viewControllers.lastObject loadView];
}

- (void)currentItemCompleted:(dViewController *)sender
{
    if (sender.editCount == 1)
    {
        [pageController setViewControllers:@[[self pageViewController:pageController viewControllerAfterViewController:sender]] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];
    }
}

- (void)end
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"What's next?", @"title of finish alert view") message:NSLocalizedString(@"Current Survey is finished, return to main page or start again?", @"message for finish alert view") delegate:self cancelButtonTitle:NSLocalizedString(@"Return", @"cancel button") otherButtonTitles:NSLocalizedString(@"Start Again", @"confirm button"), nil];
    [alert show];
}

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0)//cancel
    {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
    else//confirm
    {
        viewControllers = nil;
        [self viewDidLoad];
        [self.view setNeedsDisplay];
    }
}
@end
